package com.chixing.ih.module.task.mapper.provider;

import com.chixing.ih.module.task.pojo.Contract;
import com.chixing.ih.module.task.pojo.Task;
import com.chixing.ih.status.IsDeleteEnum;
import com.chixing.ih.status.TaskEnum;
import com.chixing.ih.status.TypeEnum;
import com.chixing.ih.util.EmptyUtil;
import org.apache.ibatis.jdbc.SQL;

import java.util.Date;
import java.util.List;

/**
 * 目的:
 *
 * @author ：zcw
 * @version: 1.0  2020/9/25 20:04
 */
public class TaskMapperProvider {

    public String saveOne() {
        return new SQL() {{
            INSERT_INTO("tbl_task");

            INTO_COLUMNS("user_id", "task_title", "talent_categories", "requirement_description", "technology",
                    "work_way", "salary", "work_cycle", "work_place", "publishing_name", "publishing_phone","update_time");
            INTO_VALUES("#{isSuerId}", "#{mandateTitle}", "#{talentCategory}", "#{requirementsDescription}",
                    "#{skillsRequirements}", "#{operationMode}", "#{salary}", "#{workCycle}", "#{workPlace}",
                    "#{isSuerName}", "#{isSuerTelephone}","#{updateTime}");

        }}.toString();
    }

    public String saveContract() {
        return new SQL() {{
            INSERT_INTO("tbl_contract");
            INTO_COLUMNS("partyA_id", "partyB_id", "task_id", "partyA_image_url", "status", "create_time");
            INTO_VALUES("#{partyAId}", "#{partyBId}", "#{taskId}", "#{partyAImageUrl}",
                    "#{status}", "#{createTime}");
        }}.toString();

    }

    public String updatePartBImg(Contract contract) {
        return new SQL() {{
            UPDATE("tbl_contract");
            SET("partyB_image_url=#{partyBImageUrl}");
            WHERE("task_id=#{taskId}");
        }}.toString();

    }

    public String delOne() {
        return new SQL() {{
            DELETE_FROM("tbl_task");
            WHERE("id = #{taskId}");
            AND();
            WHERE("type_status = " + TypeEnum.ONE.status);
            AND();
            WHERE("task_status = " + TaskEnum.ZERO.status);
            AND();
            WHERE("is_deleted = " + IsDeleteEnum.ZERO.status);
        }}.toString();
    }

    public String delAll() {
        return new SQL() {{
            DELETE_FROM("tbl_task");
            WHERE("user_id = #{isSuerId}");
            AND();
            WHERE("type_status = " + TypeEnum.ONE.status);
            AND();
            WHERE("is_deleted = " + IsDeleteEnum.ZERO.status);
            AND();
            WHERE("task_status = " + TaskEnum.ZERO.status);
        }}.toString();
    }

    public String updateOne(Task task) {
        return new SQL() {{
            UPDATE("tbl_task");
            if (!EmptyUtil.isEmpty(task.getIsSuerId())) {
                SET("user_id=#{isSuerId}");
            }
            if (!EmptyUtil.isEmpty(task.getMandateTitle())) {
                SET("task_title=#{mandateTitle}");
            }
            if (!EmptyUtil.isEmpty(task.getTalentCategory())) {
                SET("talent_categories=#{talentCategory}");
            }
            if (!EmptyUtil.isEmpty(task.getRequirementsDescription())) {
                SET("requirement_description=#{requirementsDescription}");
            }
            if (!EmptyUtil.isEmpty(task.getSkillsRequirements())) {
                SET("technology=#{skillsRequirements}");
            }
            if (!EmptyUtil.isEmpty(task.getOperationMode())) {
                SET("work_way=#{operationMode}");
            }
            if (!EmptyUtil.isEmpty(task.getSalary())) {
                SET("salary=#{salary}");
            }
            if (!EmptyUtil.isEmpty(task.getWorkCycle())) {
                SET("work_cycle=#{workCycle}");
            }
            if (!EmptyUtil.isEmpty(task.getWorkPlace())) {
                SET("work_place=#{workPlace}");
            }
            if (!EmptyUtil.isEmpty(task.getIsSuerName())) {
                SET("publishing_name=#{isSuerName}");
            }
            if (!EmptyUtil.isEmpty(task.getIsSuerTelephone())) {
                SET("publishing_phone=#{isSuerTelephone}");
            }
            if (!EmptyUtil.isEmpty(task.getTaskStatus())) {
                SET("task_status = #{taskStatus}");
            }
            if (!EmptyUtil.isEmpty(task.getLogicalDeletion())) {
                SET("is_deleted = #{logicalDeletion}");
            }
            if (!EmptyUtil.isEmpty(task.getCreateTime())) {
                SET("create_time = #{createTime}");
            }
            if (!EmptyUtil.isEmpty(task.getUpdateTime())) {
                SET("update_time = #{updateTime}");
            }
            if (!EmptyUtil.isEmpty(task.getRefreshing())) {
                SET("is_refresh = #{refreshing}");
            }
            WHERE("id = #{id}");
        }}.toString();
    }

    public String findOne() {
        return new SQL() {{
            SELECT("t.id AS id,t.user_id AS isSuerId,t.task_title AS mandateTitle,t.talent_categories AS talentCategory,t.requirement_description AS requirementsDescription,t.technology AS skillsRequirements,t.work_way AS operationMode,t.salary AS salary,t.work_cycle AS workCycle,t.work_place AS workPlace,t.publishing_name AS isSuerName,t.publishing_phone isSuerTelephone,t.task_status AS taskStatus,t.type_status AS releaseStatus,t.is_deleted AS logicalDeletion,t.create_time AS createTime, t.update_time AS updateTime,t.is_sign AS marked,t.is_refresh AS refreshing,u.nick_name AS receiveName,i.image_url AS receiveImageUrl");
            FROM("tbl_task t JOIN tbl_user u ON t.user_id = u.user_id JOIN tbl_user_image i ON u.user_id = i.user_id ");
            WHERE("t.id=#{id}");
        }}.toString();
    }

    public String findTaskOne() {
        return new SQL() {{
            SELECT("t.id AS id,t.user_id AS isSuerId,t.task_title AS mandateTitle,t.talent_categories AS talentCategory,t.requirement_description AS requirementsDescription,t.technology AS skillsRequirements,t.work_way AS operationMode,t.salary AS salary,t.work_cycle AS workCycle,t.work_place AS workPlace,t.publishing_name AS isSuerName,t.publishing_phone isSuerTelephone,t.task_status AS taskStatus,t.type_status AS releaseStatus,t.is_deleted AS logicalDeletion,t.create_time AS createTime, t.update_time AS updateTime,t.is_sign AS marked,t.is_refresh AS refreshing,u.nick_name AS receiveName,i.image_url AS receiveImageUrl");
            FROM("tbl_task t JOIN tbl_user u ON t.user_id = u.user_id JOIN tbl_user_image i ON u.user_id = i.user_id ");
            WHERE("t.id=#{id}");
            AND();
            WHERE("task_status = " + TaskEnum.ZERO.status);
            AND().WHERE("type_status = " + TypeEnum.ONE.status);
        }}.toString();
    }

    public String findAccepting() {
        return new SQL() {{
            SELECT("tbl_task.*,tbl_receive_task.receive_task_status");
            FROM("tbl_task");
            JOIN("tbl_receive_task on tbl_task.id = tbl_receive_task.task_id");
            WHERE("tbl_receive_task.receive_task_user_id = #{userId}");
            AND();
            WHERE("is_deleted = " + IsDeleteEnum.ZERO.status);
            AND().WHERE("task_status = " + TaskEnum.ZERO.status);
        }}.toString();
    }


    public String findBySendTaskTypeStatus() {
        return new SQL() {{
            SELECT("*");
            FROM("tbl_task");
            WHERE("user_id = #{userId}");
            AND();
            WHERE("type_status = " + TypeEnum.ZERO.status);
        }}.toString();
    }


    public String findBySendTaskStatus() {
        return new SQL() {{
            SELECT("*");
            FROM("tbl_task");
            WHERE("user_id = #{userId}");
            AND();
            WHERE("task_status = " + TaskEnum.ZERO.status);
            AND().WHERE("type_status = " + TypeEnum.ONE.status);
        }}.toString();
    }


    public String findAllApplication() {
        return new SQL() {{
            SELECT("receive_task_user_id");
            FROM("tbl_receive_task");
            WHERE("task_id = #{taskId}");
        }}.toString();
    }


    public String findByReceiveTaskUserId() {
        return new SQL() {{
            SELECT("*");
            FROM("tbl_task");
            WHERE("user_id = #{userId}");
            AND().WHERE("type_status != " + TypeEnum.ZERO.status);
            AND();
            WHERE("is_deleted = " + IsDeleteEnum.ZERO.status);
        }}.toString();
    }

    public String findByTaskId() {
        return new SQL() {{
            SELECT("*");
            FROM("tbl_user");
            WHERE("user_id in (select user_id from tbl_task where id = #{taskId})");
        }}.toString();
    }


    public String findAll(String talentCategories, String workPlace, Long workWay, Integer sort) {
        return new SQL() {{
            SELECT("tbl_task.*,tbl_user.nick_name,tbl_user.user_id,tbl_user.user_email,tbl_user_image.image_url");
            FROM("tbl_task");
            JOIN("tbl_user on tbl_task.user_id = tbl_user.user_id");
            JOIN("tbl_user_image on  tbl_user_image.user_id = tbl_user.user_id");
            if (!EmptyUtil.isEmpty(talentCategories)) {
                WHERE("talent_categories = #{talentCategories}");
            }
            if (!EmptyUtil.isEmpty(workPlace)) {
                WHERE("work_place = #{workPlace}");
            }
            if (!EmptyUtil.isEmpty(workWay)) {
                WHERE("work_way = #{workWay}");
            }
            if (!EmptyUtil.isEmpty(sort)) {
                if (sort == 1) {
                    ORDER_BY("salary asc");
                }
                if (sort == 2) {
                    ORDER_BY("salary desc");
                }
            }
            ORDER_BY("create_time desc");
            WHERE("type_status=" + TypeEnum.ONE.status);
            AND().WHERE("task_status = " + TaskEnum.ZERO.status);

        }}.toString();
    }


    public String findCount() {
        return new SQL() {{
            SELECT("count(*)");
            FROM("tbl_receive_task");
            WHERE("task_id = #{taskId}");
        }}.toString();
    }


    public String insertDelivery() {
        return new SQL() {{
            INSERT_INTO("tbl_receive_task");
            INTO_COLUMNS("task_id", "receive_task_user_id");
            INTO_VALUES("#{taskId}", "#{userId}");
        }}.toString();
    }

    public String taskTotalNumber() {
        return new SQL() {{
            SELECT("count(1)");
            FROM("tbl_task");
        }}.toString();
    }

    public String contract() {
        return new SQL() {{
            SELECT("id,task_title,talent_categories,salary,work_cycle");
            FROM("tbl_task");
            WHERE("id= #{taskId}");
        }}.toString();
    }

    public String contracts() {
        return new SQL() {{
            SELECT("*");
            FROM("tbl_contract");
            WHERE("task_id= #{taskId}");
        }}.toString();
    }

    public String taskExist() {
        return new SQL() {{
            SELECT("count(1)");
            FROM("tbl_contract");
            WHERE("task_id= #{taskId}");
        }}.toString();
    }

    public String contractStatus() {
        return new SQL() {{
            SELECT("receive_task_status");
            FROM("tbl_receive_task");
            WHERE("task_id= #{taskId}");
            AND().WHERE("receive_task_user_id = #{receiveTaskId}");
        }}.toString();
    }

    public String findExist() {
        return new SQL() {{
            SELECT("count(1)");
            FROM("tbl_receive_task");
            WHERE("task_id= #{taskId}");
            AND().WHERE("receive_task_user_id = #{userId}");
        }}.toString();
    }

    public String getCitys() {
        return new SQL() {{
            SELECT("region_city AS city");
            FROM("tbl_work");
            WHERE("is_deleted =0 AND region_city IS NOT NULL");
            GROUP_BY("region_city");
        }}.toString();
    }

    public String getTalentCategory() {
        return new SQL() {{
            SELECT("user_career AS talentCategory");
            FROM("tbl_user");
            WHERE("is_deleted =0 AND user_career IS NOT NULL");
            GROUP_BY("user_career");
        }}.toString();
    }

    public String getOneTask() {
        return new SQL() {{
            SELECT("task_title AS taskTitle,talent_categories AS talentCategories,technology AS technology,work_cycle AS workCycle,salary AS salary,work_place AS workPlace,publishing_name AS publishingName,publishing_phone AS publishingPhone");
            FROM("tbl_task");
            WHERE("is_deleted =0 AND id=#{id}");
        }}.toString();
    }

}
